﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Petroleum.Tool;
using HZH_Controls.Controls;
using Petroleum.DB.Model;
using Petroleum.Model;
using HZH_Controls.Forms;
using Newtonsoft.Json;
using System.Data.SQLite;

namespace Petroleum.UC
{
    /// <summary>
    /// 套管
    /// </summary>
    public partial class Step4 : UserControl
    {
        FormMain main = new FormMain();
        bool hassave = false;
        List<Casing> list = new List<Casing>();
        Casing currentCasing = new Casing();
        public Step4(FormMain main)
        {
            InitializeComponent();
            this.main = main;
            ToolStripManager.Renderer = new HZH_Controls.Controls.ProfessionalToolStripRendererEx();
            ToolStripManager.Renderer = new ProfessionalToolStripRendererEx();
        }
        /// <summary>
        /// 重填
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnClear_BtnClick(object sender, EventArgs e)
        {
            foreach (Control item in groupLeft.Controls)
            {
                if (item.GetType() == typeof(UCTextBoxEx))
                {
                    UCTextBoxEx uCText = item as UCTextBoxEx;
                    uCText.InputText = "";
                }
                if (item.GetType() == typeof(TextBox))
                {
                    TextBox uCText = item as TextBox;
                    uCText.Text = "";
                }
            }
        }
        private void ucBtnExt2_BtnClick(object sender, EventArgs e)
        {
            this.verificationComponent1.Verification();
            if (lstErrorControl.Count == 0)
            {
                if (currentCasing != null && !string.IsNullOrEmpty(currentCasing.SpudNo))
                {
                    list.Remove(currentCasing);
                }
                Casing casing = new Casing()
                {
                    SpudNo = txtSpudNo.InputText,
                    HoleSequence = txtHoleSequence.InputText,
                    HoleDepth = txtHoleDepth.InputText,
                    //HoleDepthTo = txtHoleDepthTo.InputText,
                    BitSize = txtBitSize.InputText,
                    CasingSize = txtCasingSize.InputText,
                    TratumofCasingShoeSet = txtTratumofCasingShoeSet.InputText,
                    CasingInterval = txtCasingInterval.InputText + "-" + txtCasingIntervalto.InputText,
                    CasingIntervalFrom = txtCasingInterval.InputText,
                    CasingIntervalTo = txtCasingIntervalto.InputText,
                    TopOfCement = txtTopOfCement.InputText,
                    LoggingItem = txtLoggingItem.InputText,
                    CementingQuality = txtCementingQuality.InputText,
                    DescriptionOfDesign = txtDescriptionOfDesign.Text,
                    order = int.Parse(txtorder.InputText)
                };
                list.Add(casing);
                this.dgv.DataSource = list;
                txtorder.InputText = (int.Parse(txtorder.InputText)+1)+"";
            }
            lstErrorControl.Clear();
            this.Invalidate(true);
        }
        private void ucDataGridView1_Load(object sender, EventArgs e)
        {
            List<DataGridViewColumnEntity> lstCulumns = new List<DataGridViewColumnEntity>();
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "SpudNo", HeadText = "Spud No", Width = 200, WidthType = SizeType.Absolute });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "HoleSequence", HeadText = "Hole Sequence", Width = 200, WidthType = SizeType.Absolute });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "HoleDepth", HeadText = "HoleDepth", Width = 200, WidthType = SizeType.Absolute });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "BitSize", HeadText = "Bit Size (mm)", Width = 200, WidthType = SizeType.Absolute });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "CasingSize", HeadText = "Casing size(mm)", Width = 200, WidthType = SizeType.Absolute });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "TratumofCasingShoeSet", HeadText = "StratumofCasingShoeSet", Width = 220, WidthType = SizeType.Absolute });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "CasingInterval", HeadText = "CasingInterval", Width = 150, WidthType = SizeType.Absolute });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "DescriptionOfDesign", HeadText = "Description Of Design", Width = 600, WidthType = SizeType.Absolute });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "order", HeadText = "order", Width = 50, WidthType = SizeType.Absolute });
            this.dgv.Columns = lstCulumns;
            this.dgv.DataSource = list;
        }
        private void btnDown_BtnClick(object sender, EventArgs e)
        {
            Session.CasingProgram.CasingList = list;
            LogHelper.WriteLog("Step4下一步，传递数量" + list.Count);
            main.AddControl(new Step4WellBoreQuality(main));
        }
        private void btnUp_BtnClick(object sender, EventArgs e)
        {
            main.AddControl(new Step3Img(main));
        }
        List<Control> lstErrorControl = new List<Control>();
        private void verificationComponent1_Verificationed(VerificationEventArgs e)
        {
            if (!e.IsVerifySuccess)
            {
                lstErrorControl.Add(e.VerificationControl);
            }
        }
        private void StripMenuEdit_Click(object sender, EventArgs e)
        {
            if (dgv.SelectRow != null)
            {
                int row = dgv.SelectRow.RowIndex;
                currentCasing = list[row];
                txtSpudNo.InputText = currentCasing.SpudNo;
                txtHoleSequence.InputText = currentCasing.HoleSequence;
                txtHoleDepth.InputText = currentCasing.HoleDepth;
                txtBitSize.InputText = currentCasing.BitSize;
                txtCasingSize.InputText = currentCasing.CasingSize;
                txtTratumofCasingShoeSet.InputText = currentCasing.TratumofCasingShoeSet;
                txtCasingInterval.InputText = currentCasing.CasingIntervalFrom;
                txtCasingIntervalto.InputText = currentCasing.CasingIntervalTo;
                txtTopOfCement.InputText = currentCasing.TopOfCement;
                txtLoggingItem.InputText = currentCasing.LoggingItem;
                txtCementingQuality.InputText = currentCasing.CementingQuality;
                txtDescriptionOfDesign.Text = currentCasing.DescriptionOfDesign;
                txtorder.InputText = currentCasing.order + "";
            }
        }
        private void btnDel_BtnClick(object sender, EventArgs e)
        {
            if (dgv.SelectRow != null)
            {
                int row = dgv.SelectRow.RowIndex;
                if (row > -1)
                {
                    list.RemoveAt(row);
                    this.dgv.DataSource = list;
                    return;
                }
            }
            FrmTips.ShowTipsWarning(this.FindForm(), "请先选择要删除的行");
        }
        private void btnword_BtnClick(object sender, EventArgs e)
        {
            var casing = main.projects.Where(n => n.TableName == TableNames.CasingProgram.ToString()).FirstOrDefault();
            if (casing != null)
            {
                Session.CasingProgram = JsonConvert.DeserializeObject<CasingProgram>(casing.TableData);
                list = Session.CasingProgram.CasingList;
                dgv.DataSource = list;
            }
        }
        private void btnsave_BtnClick(object sender, EventArgs e)
        {
            if (!hassave)
            {
                Session.CasingProgram.CasingList = list;
            }
            SQLiteDataReader reader = main.sqliteHelper.ReadFullTable("SessionData");
            var projects = EntityTranslate.Translate<SessionData>(reader);
            var FormationPressure = projects.Where(n => n.TableName == TableNames.CasingProgram.ToString()).FirstOrDefault();
            if (FormationPressure != null)
                SQLiteHelper.SaveData(main.sqliteHelper, TableNames.CasingProgram.ToString(), JsonConvert.SerializeObject(Session.CasingProgram));
            else
                SQLiteHelper.SaveData(main.sqliteHelper, TableNames.CasingProgram.ToString(), JsonConvert.SerializeObject(Session.CasingProgram), 1);
            FrmTips.ShowTipsSuccess(new FormMain(), "数据保存成功！");
        }
    }
}
